package com.hongxinguoji.manager.article.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hongxinguoji.manager.article.mapper.HxArticleCatMapper;
import com.hongxinguoji.manager.article.pojo.HxArticleCat;
import com.hongxinguoji.manager.article.pojo.HxArticleCatExample;
import com.hongxinguoji.manager.article.pojo.HxArticleCatExample.Criteria;
import com.hongxinguoji.manager.article.service.IArticleCatService;
import com.hongxinguoji.manager.common.util.EasyUITreeNode;

@Service
public class ArticleCatService implements IArticleCatService {

	@Autowired
	private HxArticleCatMapper articleCatMapper;

	@Override
	public List<EasyUITreeNode> getArticleCatList(long parentId) {

		// 设置查询条件
		HxArticleCatExample example = new HxArticleCatExample();
		Criteria criteria = example.createCriteria();
		criteria.andParentIdEqualTo(parentId);
		//查询状态可用。（1，正常，0，删除）
		criteria.andStatusEqualTo(1);

		// 执行查询
		List<HxArticleCat> list = articleCatMapper.selectByExample(example);
		List<EasyUITreeNode> resultList = new ArrayList<>();

		for (HxArticleCat articleCat : list) {
			EasyUITreeNode node = new EasyUITreeNode();
			node.setId(articleCat.getId());
			node.setText(articleCat.getName());
			// 如果此节点下有子节点那就是"closed"，如果没有子节点就是"open"
			node.setState(articleCat.getIsParent() == 1 ? "closed" : "open");
			resultList.add(node);
		}
		//返回结果
		return resultList;
	}

}
